<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'btn:over:apply'">
          <Icon icon="fluent:add-12-filled" /> {{ t('发起申请') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleResultForm({ id: record.id })">
          {{ record.projectname }}
        </a>
      </template>

      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)">
          <span style="text-decoration: underline; margin-right: 5px">查看</span>
        </a>
        {{ record.tercode }} -->
        <a @click="handleDetailsForm(record)">
          {{ record.tercode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <resultForm @register="registerResultDrawer" @success="handleSuccess" />
    
    <BpmRuntimeTrace @register="registerTraceModal" />
    <component
      :is="inputFormComponent"
      v-model:visible="inputFormVisible"
      @register="registerDrawerBpmMyTask"
      @success="handleSuccess"
    />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjOverPmTerminationList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent,ref,  ComponentOptions,shallowRef, onMounted } from 'vue';
  import { bpmTaskDefForm } from '/@/api/bpm/myTask';
  import { dynamicImport } from '/@/router/helper/routeHelper';
  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  import qs from 'qs';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmTerminationDelete, pmTerminationListData } from '/@/api/proj/over/pmTermination';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import resultForm from './resultForm.vue';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('proj.over.pmTermination');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('项目终止管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('项目编号'),
        field: 'tercode',
        component: 'Input',
        // defaultValue: userStore.getProjecte.code
      },
      {
        label: t('项目名称'),
        field: 'projectname',
        component: 'Input',
      },
      {
        label: t('申请人'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('申请时间'),
        field: 'createtime',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('终止人'),
        field: 'terminationid',
        component: 'Input',
      },
      {
        label: t('终止状态'),
        field: 'termination',
        component: 'Select',
        componentProps: {
          dictType: 'zf_proj_xmzz_status',
          onChange: handleSuccess,
        },
      },
      {
        label: t('流程状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
    fieldMapToTime: [['createtime', ['createtime_gte', 'createtime_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('终止状态'),
      dataIndex: 'termination',
      key: 'a.termination',
      sorter: true,
      width: 90,
      align: 'center',
      dictType: 'zf_proj_xmzz_status',
      fixed: 'left',
    },
    {
      title: t('项目编号'),
      dataIndex: 'tercode',
      key: 'a.tercode',
      sorter: true,
      width: 160,
      align: 'left',
      slot: 'projectno',
      fixed: 'left',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.projectname',
      sorter: true,
      width: 260,
      slot: 'firstColumn',
      align: 'left',
    },
    {
      title: t('申请人'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('申请时间'),
      dataIndex: 'createtime',
      key: 'a.createtime',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('申请原因'),
      dataIndex: 'remarkes',
      key: 'a.remarkes',
      sorter: true,
      width: 300,
      align: 'left',
    },
    {
      title: t('终止人'),
      dataIndex: 'terminationid',
      key: 'a.terminationid',
      sorter: true,
      width: 80,
      align: 'left',
    },

    {
      title: t('终止原因'),
      dataIndex: 'terminationremarkes',
      key: 'a.terminationremarkes',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('流程状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 90,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    align: 'left',
    actions: (record: Recordable) => [
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
      {
        icon: 'mdi:checkbox-marked-outline',
        title: t('任务办理'),
        onClick: handleBpmMyTask.bind(this, { id: record.id }),
        ifShow: () => record.status == '4' || record.status == '5' || record.status == '6',
      },
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑项目终止'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'proj:over:pmTermination:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除项目终止'),
        popConfirm: {
          title: t('是否确认删除项目终止'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'btn:over:delete',
        ifShow: () => record.status == '9',
      },
      
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerResultDrawer, { openDrawer:openResultDrawer }] = useDrawer();

  
  const [registerTable, { reload, getForm }] = useTable({
    api: pmTerminationListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        // projectno: code,
        id: record.projInfo.id,
        code: record.tercode,
        tabTitle: `项目详情(${record.tercode})`,
      },
    });
  }

  const [registerDrawerBpmMyTask, { setDrawerData }] = useDrawer();
  const inputFormComponent = shallowRef<Nullable<any>>(null);
  const inputFormVisible = ref<Boolean>(false);

  async function handleBpmMyTask(record: Recordable) {
    const data = await bpmTaskDefForm({ formKey: 'xm_over', bizKey: record.id });
    if (data.result == 'true') {
      const url = data.pcUrl;
      const idx = url.indexOf('?');
      // component
      let component: ReturnType<typeof defineComponent>;
      const compStr = idx == -1 ? url : url.substring(0, idx);
      if (compStr && compStr != '') {
        const imp = dynamicImport(compStr);
        if (imp) component = createAsyncComponent(imp);
      }
      // params
      let params = {};
      const paramStr = idx == -1 ? '' : url.substring(idx + 1);
      if (paramStr && paramStr != '') {
        params = qs.parse(paramStr);
      }
      // open
      if (component) {
        inputFormComponent.value = component;
        inputFormVisible.value = true;
        setDrawerData(params);
      }
    } else {
      showMessage(data.message);
    }
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleResultForm(record: Recordable) {
    openResultDrawer(true, record);
  }

  

  async function handleDelete(record: Recordable) {
    const res = await pmTerminationDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'xm_over', bizKey: record.id });
  }

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      tercode: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      tercode: userStore.getProjecte.code,
    });
  }
</script>
